package com.spc.car.data.config;

import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.MapUtils;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

import java.util.Map;
import java.util.Properties;

@Data
@EqualsAndHashCode(callSuper = true)
public class DruidPoolProperties extends DataSourceProperties {

    private int initialSize;

    private int minIdle;

    private int maxActive;

    private int maxWait;

    private int timeBetweenEvictionRunsMillis;

    private int minEvictableIdleTimeMillis;

    private String validationQuery;

    private boolean testWhileIdle;

    private boolean testOnBorrow;

    private boolean testOnReturn;

    private boolean poolPreparedStatements;

    private int maxPoolPreparedStatementPerConnectionSize;

    private String filters;

    private Properties connectionProperties;

    private boolean useGlobalDataSourceStat;

    public Properties toProperties() {
        final String prefix = "druid.";
        Map<String, Object> beanMap = BeanUtils.beanToMap(this);
        Properties properties = new Properties();
        if (MapUtils.isEmpty(beanMap)) {
            return properties;
        }
        for (Map.Entry<String, Object> entry : beanMap.entrySet()) {
            if (null != entry.getValue()) {
                properties.setProperty(prefix + entry.getKey(), entry.getValue().toString());
            }
        }
        return properties;
    }
}
